package com.bskyb.rss.widget;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import com.bskyb.rss.R;
import com.bskyb.rss.configure.RssFeedWidgetConfiguration;
import com.bskyb.rss.service.RssReaderService;

public class SkyNewsReaderAppWidget extends AppWidgetProvider {
	public static final String URI_SCHEME = "newsreader_widget";
	static final String TAG = "SkyNewsReaderAppWidget";

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		Log.d("SkyNewsReaderAppWidget", "onUpdate Called with size.."
				+ appWidgetIds.length);
		super.onUpdate(context, appWidgetManager, appWidgetIds);
	}

	
	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		Log.d(TAG, "onDelete()");

		for (int appWidgetId : appWidgetIds) {
			// stop alarm
			Intent widgetUpdate = new Intent(context,
					SkyNewsReaderAppWidget.class);
			widgetUpdate.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
			widgetUpdate.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
					new int[] { appWidgetId });
			widgetUpdate.setData(Uri.withAppendedPath(Uri.parse(URI_SCHEME
					+ "://widget/id/"), String.valueOf(appWidgetId)));
			PendingIntent newPending = PendingIntent.getBroadcast(context, 0,
					widgetUpdate, PendingIntent.FLAG_UPDATE_CURRENT);
			
			AlarmManager alarms = (AlarmManager) context
					.getSystemService(Context.ALARM_SERVICE);
			alarms.cancel(newPending);

			// remove preference
			Log.d("SkyNewsReaderAppWidget", "Removing preference for id "
					+ appWidgetId);
			removePreference(context, appWidgetId,
					R.id.widget_configuration_dropdown);
			removePreference(context, appWidgetId,
					R.id.widget_configuration_siteselection_dropdown);
			removePreference(context, appWidgetId,
					R.id.widget_configuration_feedrefresheditbutton);
		}

		super.onDeleted(context, appWidgetIds);
	}

	private void removePreference(Context context, int appWidgetId,
			int layoutWidgetId) {
		SharedPreferences config = context.getSharedPreferences(
				RssFeedWidgetConfiguration.PREFS_NAME, 0);
		SharedPreferences.Editor configEditor = config.edit();
		configEditor.remove(RssFeedWidgetConfiguration.PREFS_NAME + appWidgetId
				+ layoutWidgetId);
		configEditor.commit();
	}

	@Override
	public void onReceive(Context context, Intent intent) {		
		Log.d(TAG, "onReceive Called");
		final String action = intent.getAction();
		Log.d("SkyNewsReaderAppWidget", "OnReceive:Action: " + action);
		if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {			
			final int appWidgetId = intent.getExtras().getInt(
					AppWidgetManager.EXTRA_APPWIDGET_ID,
					AppWidgetManager.INVALID_APPWIDGET_ID);
			if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
				this.onDeleted(context, new int[] { appWidgetId });
			}
		} else if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)) {

			if (!URI_SCHEME.equals(intent.getScheme())
					|| intent.getStringExtra("unique") != null) {

				final int[] appWidgetIds = intent.getExtras().getIntArray(
						AppWidgetManager.EXTRA_APPWIDGET_IDS);
				for (int mAppWidgetId : appWidgetIds) {
					Bundle extras = intent.getExtras();
					if (extras != null) {
						int[] mAppWidgetIdArray = extras
								.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
						mAppWidgetId = mAppWidgetIdArray[0];
					}
					Intent newIntent = new Intent(context,
							RssReaderService.class);
					newIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
							mAppWidgetId);
					Log.d(TAG, "onReceive Called with action as appwidget update");;
					context.startService(newIntent);

				}

			}

			super.onReceive(context, intent);

		} else {
			super.onReceive(context, intent);
		}

	}

}
